package com.neuq.animaladoption.controller;

import com.neuq.animaladoption.pojo.PageBean;
import com.neuq.animaladoption.pojo.Result;
import com.neuq.animaladoption.pojo.Shelter;
import com.neuq.animaladoption.service.ShelterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shelter")
public class ShelterController {

    @Autowired
    private ShelterService shelterService;

    @PostMapping
    public Result add(@RequestBody @Validated(Shelter.Add.class) Shelter shelter){
        shelterService.add(shelter);
        return Result.success();
    }

    @GetMapping
    public Result<PageBean<Shelter>> list(
            Integer pageNum,
            Integer pageSize,
            String cityAddress){
        PageBean<Shelter> pb = shelterService.list(pageNum,pageSize,cityAddress);
        return Result.success(pb);
    }

    @GetMapping("/detail")
    public Result<Shelter> detail(Integer id){
        Shelter shelter = shelterService.findById(id);
        return Result.success(shelter);
    }

    @PutMapping
    public Result update(@RequestBody @Validated(Shelter.Update.class) Shelter shelter){
        shelterService.update(shelter);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(Integer id){
        shelterService.deleteById(id);
        return Result.success();
    }
}
